﻿<span style='font-size: 11px; color: #000000; background: #ffffff;'>
    <pre>
<span style='color:#800000; font-weight:bold; '>protected</span> <span style='color:#800000; font-weight:bold; '>void</span> convertToPdfButton_Click<span style='color:#808030; '>(</span><span style='color:#800000; font-weight:bold; '>object</span> sender<span style='color:#808030; '>,</span> EventArgs e<span style='color:#808030; '>)</span>
<span style='color:#800080; '>{</span>
    <span style='color:#696969; '>// Create a HTML to PDF converter object with default settings</span>
    HtmlToPdfConverter htmlToPdfConverter <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>new</span> HtmlToPdfConverter<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Set license key received after purchase to use the converter in licensed mode</span>
    <span style='color:#696969; '>// Leave it not set to use the converter in demo mode</span>
    htmlToPdfConverter<span style='color:#808030; '>.</span>LicenseKey <span style='color:#808030; '>=</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>4W9+bn19bn5ue2B+bn1/YH98YHd3d3c=</span><span style='color:#800000; '>"</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Set an adddional delay in seconds to wait for JavaScript or AJAX calls after page load completed</span>
    <span style='color:#696969; '>// Set this property to 0 if you don't need to wait for such asynchcronous operations to finish</span>
    htmlToPdfConverter<span style='color:#808030; '>.</span>ConversionDelay <span style='color:#808030; '>=</span> <span style='color:#008c00; '>2</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Enable footer in the generated PDF document</span>
    htmlToPdfConverter<span style='color:#808030; '>.</span>PdfDocumentOptions<span style='color:#808030; '>.</span>ShowFooter <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>true</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Optionally add a space between footer and the page body</span>
    <span style='color:#696969; '>// Leave this option not set for no spacing</span>
    htmlToPdfConverter<span style='color:#808030; '>.</span>PdfDocumentOptions<span style='color:#808030; '>.</span>BottomSpacing <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>float</span><span style='color:#808030; '>.</span>Parse<span style='color:#808030; '>(</span>footerSpacingTextBox<span style='color:#808030; '>.</span>Text<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Set the footer height in points</span>
    htmlToPdfConverter<span style='color:#808030; '>.</span>PdfFooterOptions<span style='color:#808030; '>.</span>FooterHeight <span style='color:#808030; '>=</span> <span style='color:#008c00; '>50</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// ----- Add HTML with Page Numbering to Footer -----</span>

    <span style='color:#696969; '>// Create a variable HTML element with page numbering</span>
    <span style='color:#800000; font-weight:bold; '>string</span> htmlStringWithPageNumbers <span style='color:#808030; '>=</span> htmlWithPageNumbersTextBox<span style='color:#808030; '>.</span>Text<span style='color:#800080; '>;</span>
    <span style='color:#800000; font-weight:bold; '>string</span> baseUrl <span style='color:#808030; '>=</span> baseUrlTextBox<span style='color:#808030; '>.</span>Text<span style='color:#800080; '>;</span>
    HtmlToPdfVariableElement footerHtmlWithPageNumbers <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>new</span> HtmlToPdfVariableElement<span style='color:#808030; '>(</span>htmlStringWithPageNumbers<span style='color:#808030; '>,</span> baseUrl<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Set the HTML element to fit the container height</span>
    footerHtmlWithPageNumbers<span style='color:#808030; '>.</span>FitHeight <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>true</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Add variable HTML element with page numbering to footer</span>
    htmlToPdfConverter<span style='color:#808030; '>.</span>PdfFooterOptions<span style='color:#808030; '>.</span>AddElement<span style='color:#808030; '>(</span>footerHtmlWithPageNumbers<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Optionally draw a line at the top of the footer</span>
    <span style='color:#800000; font-weight:bold; '>if</span> <span style='color:#808030; '>(</span>drawFooterLineCheckBox<span style='color:#808030; '>.</span>Checked<span style='color:#808030; '>)</span>
    <span style='color:#800080; '>{</span>
        <span style='color:#696969; '>// Calculate the footer width based on PDF page size and margins</span>
        <span style='color:#800000; font-weight:bold; '>float</span> footerWidth <span style='color:#808030; '>=</span> htmlToPdfConverter<span style='color:#808030; '>.</span>PdfDocumentOptions<span style='color:#808030; '>.</span>PdfPageSize<span style='color:#808030; '>.</span>Width <span style='color:#808030; '>-</span>
                    htmlToPdfConverter<span style='color:#808030; '>.</span>PdfDocumentOptions<span style='color:#808030; '>.</span>LeftMargin <span style='color:#808030; '>-</span> htmlToPdfConverter<span style='color:#808030; '>.</span>PdfDocumentOptions<span style='color:#808030; '>.</span>RightMargin<span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Create a line element for the top of the footer</span>
        LineElement footerLine <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>new</span> LineElement<span style='color:#808030; '>(</span><span style='color:#008c00; '>0</span><span style='color:#808030; '>,</span> <span style='color:#008c00; '>0</span><span style='color:#808030; '>,</span> footerWidth<span style='color:#808030; '>,</span> <span style='color:#008c00; '>0</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Set line color</span>
        footerLine<span style='color:#808030; '>.</span>ForeColor <span style='color:#808030; '>=</span> Color<span style='color:#808030; '>.</span>Gray<span style='color:#800080; '>;</span>

        <span style='color:#696969; '>// Add line element to the bottom of the footer</span>
        htmlToPdfConverter<span style='color:#808030; '>.</span>PdfFooterOptions<span style='color:#808030; '>.</span>AddElement<span style='color:#808030; '>(</span>footerLine<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
    <span style='color:#800080; '>}</span>

    <span style='color:#696969; '>// Convert the HTML page to a PDF document in a memory buffer</span>
    <span style='color:#800000; font-weight:bold; '>byte</span><span style='color:#808030; '>[</span><span style='color:#808030; '>]</span> outPdfBuffer <span style='color:#808030; '>=</span> htmlToPdfConverter<span style='color:#808030; '>.</span>ConvertUrl<span style='color:#808030; '>(</span>urlTextBox<span style='color:#808030; '>.</span>Text<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Send the PDF as response to browser</span>

    <span style='color:#696969; '>// Set response content type</span>
    Response<span style='color:#808030; '>.</span>AddHeader<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>Content-Type</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> <span style='color:#800000; '>"</span><span style='color:#0000e6; '>application/pdf</span><span style='color:#800000; '>"</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Instruct the browser to open the PDF file as an attachment or inline</span>
    Response<span style='color:#808030; '>.</span>AddHeader<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>Content-Disposition</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> String<span style='color:#808030; '>.</span>Format<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0000e6; '>attachment; filename=Page_Numbers_in_HTML.pdf; size={0}</span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> outPdfBuffer<span style='color:#808030; '>.</span>Length<span style='color:#808030; '>.</span>ToString<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// Write the PDF document buffer to HTTP response</span>
    Response<span style='color:#808030; '>.</span>BinaryWrite<span style='color:#808030; '>(</span>outPdfBuffer<span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>

    <span style='color:#696969; '>// End the HTTP response and stop the current page processing</span>
    Response<span style='color:#808030; '>.</span>End<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
<span style='color:#800080; '>}</span>
</pre>
</span>